package com.ruoyi.web.controller.tool;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class ListFilesWithDates {

    public static void main(String[] args) {
        // 指定要遍历的目录路径
        //String directoryPath = "/path/to/your/directory";
        String directoryPath = "D:\\ruoyi\\uploadPath\\upload\\code";

        // 使用 File 类获取目录对象
        File directory = new File(directoryPath);

        // 检查路径是否是一个目录
        if (!directory.isDirectory()) {
            System.out.println("The specified path is not a directory.");
            return;
        }

        // 获取目录下的所有文件和子目录
        File[] files = directory.listFiles();

        if (files != null) {
            for (File file : files) {
                // 获取文件的绝对路径
                Path filePath = Paths.get(file.getAbsolutePath());

                // 获取文件的详细信息
                try {
                    BasicFileAttributes attrs = Files.readAttributes(filePath, BasicFileAttributes.class);

                    // 获取文件的修改时间
                    Date lastModifiedTime = new Date(attrs.lastModifiedTime().toMillis());
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String formattedDate = sdf.format(lastModifiedTime);

                    // 打印文件名和修改时间
                    System.out.println("File: " + file.getName() + ", Modified Date: " + formattedDate);

                } catch (IOException e) {
                    System.err.println("Error reading attributes for file: " + file.getName());
                    e.printStackTrace();
                }
            }
        } else {
            System.out.println("The directory is empty or an error occurred.");
        }
    }
}
